package com.example.photoview;

import com.example.showImage.ImageController;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Optional;
/**
 * 完成图片删除操作
 */
public class DeleteAction {
   private HomeController homeController=(HomeController) ControllerUtil.controllers.get(HomeController.class.getSimpleName());
    private ImageController imageController;
    ArrayList<ImageModel> sourceList = SelectionModel.getImageModels();//存放图片本身的List

        //将本类的实例添加到全局映射中
       // ControllerUtil.controllers.put(this.getClass().getSimpleName(), this);

    /**
     *  删除功能调用：生成对话框
     * @param imageBox
     */
    public DeleteAction(ImageBox imageBox) {
        if (sourceList.isEmpty()) {
            SelectedModel.setSourcePath(imageBox.getImageModel().getImageFilePath());
            Alert alert = new Alert(Alert.AlertType.WARNING);
            alert.setTitle("确定删除");
            alert.setHeaderText(null);
            alert.setContentText("确定要删除这" + imageBox.getImageModel().getImageName() + "文件吗？");
            Optional<ButtonType> result = alert.showAndWait();
            if (result.get() == ButtonType.OK) {
                int n;
                n = SelectedModel.deleteImage();
                if (n > 0) {

                    if (imageController != null) {
                        try {
                            imageController.nextImg();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                }
               homeController.refreshImagesList(homeController.getSortComboBox().getValue());

            } else {
                alert.close();
            }
        } else {
            SelectedModel.setSourcePath(sourceList);
            Alert alert = new Alert(Alert.AlertType.WARNING);
            alert.setTitle("确定删除");
            alert.setHeaderText(null);
            alert.setContentText("确定要删除这" + sourceList.size() + "个文件吗？");
            Optional<ButtonType> result = alert.showAndWait();
            if (result.get() == ButtonType.OK) {
                int n;
                n = SelectedModel.deleteImage();
                if (n > 0) {

                    if (imageController != null) {
                        try {
                            imageController.nextImg();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                   homeController.refreshImagesList(homeController.getSortComboBox().getValue());


                }
            }

        }
    }
}
